package com.example.onlinetaxi.ui.activity.Driver.MainPage.OrderFragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.OrderCallBack.CompletedOrderCallBack;
import com.example.onlinetaxi.callback.OrderCallBack.GetOrderCallBack;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.dto.OrderDto.OrderDTO;
import com.example.onlinetaxi.ui.activity.CommentPage.CommentActivity;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.util.TimeUtil;
import com.example.onlinetaxi.view.Adapter.CompletedOrderAdapter;
import com.example.onlinetaxi.view.Adapter.DriverCompleteOrderAdapter;
import com.example.onlinetaxi.view.Dialog.MyDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DriverCompleteOrderFragment extends Fragment {

    private View rootView;
    // 无订单提醒
    private TextView empty;
    // 订单列表
    private List<CompletedOrderDto> orderList = new ArrayList<>();
    // 重新获取订单
    private GetOrderCallBack getOrderCallBack;
    // 请求参数
    private Map<String, String> map = new HashMap<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_driver_complete_order, container, false);

        initView();

        initEvent();

        return rootView;
    }

    private void initEvent() {
        // 初始化订单列表控件
        initRecyclerView();

        // 初始化空页面
        if (orderList.size() != 0) {
            empty.setVisibility(View.INVISIBLE);
        } else {
            empty.setVisibility(View.VISIBLE);
        }
    }

    private void initRecyclerView() {
        // 找到布局文件中的recyclerView
        RecyclerView recyclerView = rootView.findViewById(R.id.driver_complete_order_list);
        /*指定RecyclerView布局方式*/
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        /*指定为LinearLayout布局*/
        recyclerView.setLayoutManager(layoutManager);

        // 设置管理器颜色配置
        DriverCompleteOrderAdapter.drawable7 = getContext().getDrawable(R.color.blue);
        DriverCompleteOrderAdapter.drawable8 = getContext().getDrawable(R.color.perfect_green);
        DriverCompleteOrderAdapter.drawable_pay_btn = getContext().getDrawable(R.drawable.blue_btn);
        DriverCompleteOrderAdapter.drawable_delete_btn = getContext().getDrawable(R.drawable.exit_app_btn_bg);

        /*适配去管理滑动显示*/
        DriverCompleteOrderAdapter adapter = new DriverCompleteOrderAdapter(orderList);

        // 设置点击回调函数
        adapter.setCompletedOrderCallBack(new CompletedOrderCallBack() {
            @Override
            public void jump(CompletedOrderDto order) {
                // 点击后进行跳转,跳转到评价界面
                // 转换时间格式
                // 设置当前订单为选中订单
                LocalStorage.chosenOrder = TimeUtil.setRightTimeFormat(order);

                startActivity(new Intent(getActivity(), CommentActivity.class));
            }

            @Override
            public void clickPayButton(CompletedOrderDto order) {
                // 发短信提醒用户进行支付
                informPay(order);
            }

            @Override
            public void clickDeleteButton(CompletedOrderDto order) {
                // 弹出确认框
                showDialog(order);
            }
        });

        /*设置适配*/
        recyclerView.setAdapter(adapter);
    }

    private void informPay(CompletedOrderDto orderDto) {
        // 向后台发送请求，通知乘客进行支付
        map.clear();
        map.put("orderId", String.valueOf(orderDto.getCompleteOrderId()));

        String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "SMS", "payMessage");

        DataResponse dataResponse = GetDataResponse.getDataResponse(response);

        if (dataResponse.getCode().equals("200")) {
            Toast.makeText(getActivity(), "已成功提醒该用户付费！", Toast.LENGTH_SHORT).show();
        } else {
            if (dataResponse.getData().containsKey("error")) {
                Toast.makeText(getActivity(), dataResponse.getData().get("error").toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void showDialog(CompletedOrderDto order) {
        View.OnClickListener onCancelClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 取消，不操作
            }
        };

        View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确认
                // 删除订单
                deleteCompleteOrder(order);

                // 刷新当前页面
                refresh();

                Toast.makeText(getActivity(), "您已删除该订单!", Toast.LENGTH_SHORT).show();
            }
        };

        MyDialog.drawable = getContext().getDrawable(R.drawable.dialog_confirm_driver);

        // 显示提示框
        MyDialog.showInfoDialog(getActivity(), "您是否要删除当前订单记录？", "取消", onCancelClickListener, "确认", onConfirmClickListener);
    }

    private void deleteCompleteOrder(CompletedOrderDto order) {
        map.clear();

        map.put("orderId", String.valueOf(order.getCompleteOrderId()));

        OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "completedOrder", "deleteOrder");
    }

    private void refresh() {
        // 重新获取order列表
        getOrderCallBack.refreshOrder();

        // 刷新当前页面
        initEvent();
    }

    private void initView() {
        empty = rootView.findViewById(R.id.driver_complete_order_empty);

        if (orderList.size() != 0) {
            empty.setVisibility(View.INVISIBLE);
        }
    }

    public void setOrderList(List<CompletedOrderDto> orderList) {
        this.orderList = orderList;
    }

    public void setGetOrderCallBack(GetOrderCallBack getOrderCallBack) {
        this.getOrderCallBack = getOrderCallBack;
    }
}